<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="qq客服的超链接" prop="qqLink">
      <el-input v-model="dataForm.qqLink" placeholder="qq客服的超链接"></el-input>
    </el-form-item>
<!--    <el-form-item label="删除状态【0删除，1存在】" prop="showStatus">-->
<!--      <el-input v-model="dataForm.showStatus" placeholder="删除状态【0删除，1存在】"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="创建时间" prop="createTime">-->
<!--      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="更新时间" prop="updateTime">-->
<!--      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="客服姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="客服姓名"></el-input>
    </el-form-item>
      <el-form-item label="是否公开" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">不公开</el-radio>
          <el-radio :label="1">公开</el-radio>
        </el-radio-group>
      </el-form-item>
    <el-form-item label="客服手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="客服手机号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          qqLink: '',
          showStatus: '',
          createTime: '',
          updateTime: '',
          name: '',
          status: '',
          phone: ''
        },
        dataRule: {
          qqLink: [
            { required: true, message: 'qq客服的超链接不能为空', trigger: 'blur' }
          ],
          showStatus: [
            { required: true, message: '删除状态【0删除，1存在】不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '客服姓名不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '是否启用【0禁用，1启用】不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '客服手机号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/service/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.qqLink = data.service.qqLink
                this.dataForm.showStatus = data.service.showStatus
                this.dataForm.createTime = data.service.createTime
                this.dataForm.updateTime = data.service.updateTime
                this.dataForm.name = data.service.name
                this.dataForm.status = data.service.status
                this.dataForm.phone = data.service.phone
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/service/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'qqLink': this.dataForm.qqLink,
                'showStatus': this.dataForm.showStatus,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'name': this.dataForm.name,
                'status': this.dataForm.status,
                'phone': this.dataForm.phone
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
